package com.ruoyi.oar.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.oar.domain.OarUserCommunity;
import com.ruoyi.oar.domain.dto.OarUserCommunityDTO;
import com.ruoyi.oar.domain.query.OarUserCommunityQuery;
import com.ruoyi.oar.mapper.OarUserCommunityMapper;
import com.ruoyi.oar.service.OarUserCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用户与社区关联网约房Service业务层处理
 * 
 * @author oar
 * @date 2022-03-04
 */
@Service
public class OarUserCommunityServiceImpl implements OarUserCommunityService
{
    @Autowired
    private OarUserCommunityMapper oarUserCommunityMapper;

    /**
     * 查询用户与社区关联网约房
     * 
     * @param userId 用户与社区关联网约房主键
     * @return 用户与社区关联网约房
     */
    @Override
    public Optional<OarUserCommunityDTO> selectOarUserCommunityByUserId(String userId) {
        return oarUserCommunityMapper.selectOarUserCommunityByUserId(userId).map(oarUserCommunity -> toDto(oarUserCommunity,true));
    }

    /**
     * 查询用户与社区关联网约房列表
     * 
     * @param oarUserCommunityQuery 用户与社区关联网约房
     * @return 用户与社区关联网约房
     */
    @Override
    public List<OarUserCommunityDTO> selectOarUserCommunityList(OarUserCommunityQuery oarUserCommunityQuery) {
        return oarUserCommunityMapper.selectOarUserCommunityList(oarUserCommunityQuery).stream().map(this::toDto).collect(Collectors.toList());
    }

    /**
     * 分页查询用户与社区关联网约房列表
     * @param oarUserCommunityQuery 用户与社区关联网约房
     * @return
     */
    @Override
    public TableDataInfo<OarUserCommunityDTO> findAll(OarUserCommunityQuery oarUserCommunityQuery) {
        List<OarUserCommunity> list = oarUserCommunityMapper.selectOarUserCommunityList(oarUserCommunityQuery);
        List<OarUserCommunityDTO> dtoList = list.stream().map(this::toDto).collect(Collectors.toList());
        return PageUtil.getDataTable(dtoList,new PageInfo(list).getTotal());
    }
    /**
     * 新增用户与社区关联网约房
     * 
     * @param oarUserCommunityDTO 用户与社区关联网约房
     * @return 结果
     */
    @Override
    public int insertOarUserCommunity(OarUserCommunityDTO oarUserCommunityDTO) {
        checkOarUserCommunity(oarUserCommunityDTO);
        return oarUserCommunityMapper.insertOarUserCommunity(toEntity(oarUserCommunityDTO));
    }

    /**
     * 修改用户与社区关联网约房
     * 
     * @param oarUserCommunityDTO 用户与社区关联网约房
     * @return 结果
     */
    @Override
    public int updateOarUserCommunity(OarUserCommunityDTO oarUserCommunityDTO) {
        checkOarUserCommunity(oarUserCommunityDTO);
        return oarUserCommunityMapper.updateOarUserCommunity(toEntity(oarUserCommunityDTO));
    }

    /**
     * 批量删除用户与社区关联网约房
     * 
     * @param userIds 需要删除的用户与社区关联网约房主键
     * @return 结果
     */
    @Override
    public int deleteOarUserCommunityByUserIds(String[] userIds) {
        return oarUserCommunityMapper.deleteOarUserCommunityByUserIds(userIds);
    }

    /**
     * 删除用户与社区关联网约房信息
     * 
     * @param userId 用户与社区关联网约房主键
     * @return 结果
     */
    @Override
    public int deleteOarUserCommunityByUserId(String userId)
    {
        return oarUserCommunityMapper.deleteOarUserCommunityByUserId(userId);
    }

    /**
    * 校验
    * @param dto
    */
    private void checkOarUserCommunity(OarUserCommunityDTO dto) {

    }

    private OarUserCommunity toEntity(OarUserCommunityDTO dto) {
        return BeanUtil.toBean(dto, OarUserCommunity.class);
    }

    private OarUserCommunityDTO toDto(OarUserCommunity entity) {
        return toDto(entity, false);
    }
    /**
     * 转DTO
     *
     * @param entity
     * @param withDetail 是否查询其他信息
     * @return
     */
    private OarUserCommunityDTO toDto(OarUserCommunity entity, Boolean withDetail) {
            OarUserCommunityDTO dto =  BeanUtil.toBean(entity, OarUserCommunityDTO.class);
        if (withDetail) {

        }
        return dto;
    }
}
